1
|
|
|
/* |
2
|
|
|
* Copyright (c) 2017-2019 Rafael da Silva Rocha. |
3
|
|
|
* |
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
5
|
|
|
* a copy of this software and associated documentation files (the |
6
|
|
|
* "Software"), to deal in the Software without restriction, including |
7
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
8
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
9
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
10
|
|
|
* the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be |
13
|
|
|
* included in all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
18
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
19
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
20
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
21
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @fileoverview The RIFFFile class. |
27
|
|
|
* @see https://github.com/rochars/wavefile |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
import { unpackString, unpack } from './parsers/binary'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* A class to perform low-level reading of RIFF/RIFX files. |
34
|
|
|
*/ |
35
|
|
|
export class RIFFFile { |
36
|
|
|
|
37
|
|
|
constructor() { |
38
|
|
|
/** |
39
|
|
|
* The container identifier. |
40
|
|
|
* 'RIFF', 'RIFX' and 'RF64' are supported. |
41
|
|
|
* @type {string} |
42
|
|
|
*/ |
43
|
|
|
this.container = ''; |
44
|
|
|
/** |
45
|
|
|
* @type {number} |
46
|
|
|
*/ |
47
|
|
|
this.chunkSize = 0; |
48
|
|
|
/** |
49
|
|
|
* The format. |
50
|
|
|
* @type {string} |
51
|
|
|
*/ |
52
|
|
|
this.format = ''; |
53
|
|
|
/** |
54
|
|
|
* A object defining the start and end of all chunks in a wav buffer. |
55
|
|
|
* @type {Object} |
56
|
|
|
*/ |
57
|
|
|
this.signature = null; |
58
|
|
|
/** |
59
|
|
|
* @type {number} |
60
|
|
|
* @protected |
61
|
|
|
*/ |
62
|
|
|
this.head = 0; |
63
|
|
|
/** |
64
|
|
|
* @type {!{bits: number, be: boolean}} |
65
|
|
|
* @protected |
66
|
|
|
*/ |
67
|
|
|
this.uInt32 = {bits: 32, be: false}; |
68
|
|
|
/** |
69
|
|
|
* The list of supported containers. |
70
|
|
|
* Any format different from RIFX will be treated as RIFF. |
71
|
|
|
* @type {!Array<string>} |
72
|
|
|
* @protected |
73
|
|
|
*/ |
74
|
|
|
this.supported_containers = ['RIFF', 'RIFX']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Read the signature of the chunks in a RIFF/RIFX file. |
79
|
|
|
* @param {!Uint8Array} buffer The file bytes. |
80
|
|
|
* @protected |
81
|
|
|
*/ |
82
|
|
|
setSignature(buffer) { |
83
|
|
|
this.head = 0; |
84
|
|
|
this.container = this.readString(buffer, 4); |
85
|
|
|
if (this.supported_containers.indexOf(this.container) === -1) { |
86
|
|
|
throw Error('Not a supported format.'); |
87
|
|
|
} |
88
|
|
|
this.uInt32.be = this.container === 'RIFX'; |
89
|
|
|
this.chunkSize = this.readUInt32(buffer); |
90
|
|
|
this.format = this.readString(buffer, 4); |
91
|
|
|
// The RIFF file signature |
92
|
|
|
this.signature = { |
93
|
|
|
chunkId: this.container, |
94
|
|
|
chunkSize: this.chunkSize, |
95
|
|
|
format: this.format, |
96
|
|
|
subChunks: this.getSubChunksIndex_(buffer) |
97
|
|
|
}; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Find a chunk by its fourCC_ in a array of RIFF chunks. |
102
|
|
|
* @param {string} chunkId The chunk fourCC_. |
103
|
|
|
* @param {boolean} [multiple=false] True if there may be multiple chunks |
104
|
|
|
* with the same chunkId. |
105
|
|
|
* @return {Object} |
106
|
|
|
* @protected |
107
|
|
|
*/ |
108
|
|
|
findChunk(chunkId, multiple=false) { |
109
|
|
|
/** @type {!Array<Object>} */ |
110
|
|
|
let chunks = this.signature.subChunks; |
111
|
|
|
/** @type {!Array<Object>} */ |
112
|
|
|
let chunk = []; |
113
|
|
|
for (let i=0; i<chunks.length; i++) { |
114
|
|
|
if (chunks[i].chunkId == chunkId) { |
115
|
|
|
if (multiple) { |
116
|
|
|
chunk.push(chunks[i]); |
117
|
|
|
} else { |
118
|
|
|
return chunks[i]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
if (chunkId == 'LIST') { |
123
|
|
|
return chunk.length ? chunk : null; |
124
|
|
|
} |
125
|
|
|
return null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Read bytes as a string from a RIFF chunk. |
130
|
|
|
* @param {!Uint8Array} bytes The bytes. |
131
|
|
|
* @param {number} maxSize the max size of the string. |
132
|
|
|
* @return {string} The string. |
133
|
|
|
* @protected |
134
|
|
|
*/ |
135
|
|
|
readString(bytes, maxSize) { |
136
|
|
|
/** @type {string} */ |
137
|
|
|
let str = ''; |
138
|
|
|
str = unpackString(bytes, this.head, this.head + maxSize); |
139
|
|
|
this.head += maxSize; |
140
|
|
|
return str; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Read a number from a chunk. |
145
|
|
|
* @param {!Uint8Array} bytes The chunk bytes. |
146
|
|
|
* @return {number} The number. |
147
|
|
|
* @protected |
148
|
|
|
*/ |
149
|
|
|
readUInt32(bytes) { |
150
|
|
|
/** @type {number} */ |
151
|
|
|
let value = unpack(bytes, this.uInt32, this.head); |
152
|
|
|
this.head += 4; |
153
|
|
|
return value; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return the sub chunks of a RIFF file. |
158
|
|
|
* @param {!Uint8Array} buffer the RIFF file bytes. |
159
|
|
|
* @return {!Array<Object>} The subchunks of a RIFF/RIFX or LIST chunk. |
160
|
|
|
* @private |
161
|
|
|
*/ |
162
|
|
|
getSubChunksIndex_(buffer) { |
163
|
|
|
/** @type {!Array<!Object>} */ |
164
|
|
|
let chunks = []; |
165
|
|
|
/** @type {number} */ |
166
|
|
|
let i = this.head; |
167
|
|
|
while(i <= buffer.length - 8) { |
168
|
|
|
chunks.push(this.getSubChunkIndex_(buffer, i)); |
169
|
|
|
i += 8 + chunks[chunks.length - 1].chunkSize; |
170
|
|
|
i = i % 2 ? i + 1 : i; |
171
|
|
|
} |
172
|
|
|
return chunks; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Return a sub chunk from a RIFF file. |
177
|
|
|
* @param {!Uint8Array} buffer the RIFF file bytes. |
178
|
|
|
* @param {number} index The start index of the chunk. |
179
|
|
|
* @return {!Object} A subchunk of a RIFF/RIFX or LIST chunk. |
180
|
|
|
* @private |
181
|
|
|
*/ |
182
|
|
|
getSubChunkIndex_(buffer, index) { |
183
|
|
|
/** @type {!Object} */ |
184
|
|
|
let chunk = { |
185
|
|
|
chunkId: this.getChunkId_(buffer, index), |
186
|
|
|
chunkSize: this.getChunkSize_(buffer, index), |
187
|
|
|
}; |
188
|
|
|
if (chunk.chunkId == 'LIST') { |
189
|
|
|
chunk.format = unpackString(buffer, index + 8, index + 12); |
190
|
|
|
this.head += 4; |
191
|
|
|
chunk.subChunks = this.getSubChunksIndex_(buffer); |
192
|
|
|
} else { |
193
|
|
|
/** @type {number} */ |
194
|
|
|
let realChunkSize = chunk.chunkSize % 2 ? |
195
|
|
|
chunk.chunkSize + 1 : chunk.chunkSize; |
196
|
|
|
this.head = index + 8 + realChunkSize; |
197
|
|
|
chunk.chunkData = { |
198
|
|
|
start: index + 8, |
199
|
|
|
end: this.head |
200
|
|
|
}; |
201
|
|
|
} |
202
|
|
|
return chunk; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Return the fourCC_ of a chunk. |
207
|
|
|
* @param {!Uint8Array} buffer the RIFF file bytes. |
208
|
|
|
* @param {number} index The start index of the chunk. |
209
|
|
|
* @return {string} The id of the chunk. |
210
|
|
|
* @private |
211
|
|
|
*/ |
212
|
|
|
getChunkId_(buffer, index) { |
213
|
|
|
this.head += 4; |
214
|
|
|
return unpackString(buffer, index, index + 4); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Return the size of a chunk. |
219
|
|
|
* @param {!Uint8Array} buffer the RIFF file bytes. |
220
|
|
|
* @param {number} index The start index of the chunk. |
221
|
|
|
* @return {number} The size of the chunk without the id and size fields. |
222
|
|
|
* @private |
223
|
|
|
*/ |
224
|
|
|
getChunkSize_(buffer, index) { |
225
|
|
|
this.head += 4; |
226
|
|
|
return unpack(buffer, this.uInt32, index + 4); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|